package com.example.dps;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class CalcDBHelper extends SQLiteOpenHelper {
	
	
	/**********************************************************************
	 * Database Variables
	 */
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "DownPaymentSaver.db";
    /********************************************************************/
    
    /********************************************************************
    *  CALC DB Variables
    *******************************************************************/
    public String CALC_TABLE_NAME = "Calculator";
    public String COLUMN_CALC_INTEREST = "interest";
    public String COLUMN_CALC_INCOME = "income";
    public String COLUMN_CALC_EXPENSES = "expenses";
    public String COLUMN_CALC_ID = "_id";
    
    public String CREATE_CALC_TABLE = "CREATE TABLE IF NOT EXISTS " + CALC_TABLE_NAME +
    		"(" +
    		COLUMN_CALC_ID + "INTEGER PRIMARY KEY,"+
    		COLUMN_CALC_INTEREST + " REAL,"+
    		COLUMN_CALC_EXPENSES + " INTEGER,"+
    		COLUMN_CALC_INCOME + " INTEGER"+
    		");";
    /*******************************************************************/
	
    
    SQLiteDatabase db;

	public CalcDBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}
	
	public CalcDBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
		this.db = getWritableDatabase();
		
		db.execSQL(CREATE_CALC_TABLE);
		
		Cursor cursor = db.rawQuery("SELECT * FROM " + CALC_TABLE_NAME, null);
		if(cursor.getCount() < 1){
			/** if empty, insert initial entry **/			
			String query = "INSERT INTO " + CALC_TABLE_NAME + " (" +
					COLUMN_CALC_INTEREST + "," +
					COLUMN_CALC_INCOME + "," + 
					COLUMN_CALC_EXPENSES + ") VALUES (0,0,0);";
			
			
			db.execSQL(query);
		}
		
		db.close();
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
	}
	
	public void updateInterestRate(double rate){
		
		this.db = getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(COLUMN_CALC_INTEREST, rate);
		
		db.update(CALC_TABLE_NAME, values, null , null);
		
		db.close();
	}
	
	public void updateIncome(int income){
		
		this.db = getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(COLUMN_CALC_INCOME, income);
		
		db.update(CALC_TABLE_NAME, values, null , null);
		
		db.close();
	}
	
	
	/**
	 * get the interest rate from the calc database
	 * @return interest rate or 0.00 if none exists
	 */
	public double selectInterestRate(){
		
		this.db = getReadableDatabase();
		
		Cursor cursor = db.rawQuery("SELECT "+COLUMN_CALC_INTEREST+" FROM "+ CALC_TABLE_NAME, null);
		
		if(cursor.moveToFirst()){
			
			double rate = cursor.getDouble(cursor.getColumnIndex(COLUMN_CALC_INTEREST));
			
			cursor.close();
			this.db.close();
			
			return rate;
			
		}
		else{
			cursor.close();
			this.db.close();
			return 0.00;
		}
		
	}
	
	public int selectIncome(){
		this.db = getReadableDatabase();
		
		Cursor cursor = db.rawQuery("SELECT "+COLUMN_CALC_INCOME+" FROM "+ CALC_TABLE_NAME, null);
		
		if(cursor.moveToFirst()){
			return cursor.getInt(cursor.getColumnIndex(COLUMN_CALC_INCOME));
		}
		else return 0;
	}
	
	public int selectExpenses(){
		
		this.db = getReadableDatabase();
		
		Cursor cursor = db.rawQuery("SELECT "+COLUMN_CALC_EXPENSES+" FROM "+ CALC_TABLE_NAME, null);
		
		if(cursor.moveToFirst()){
			return cursor.getInt(cursor.getColumnIndex(COLUMN_CALC_EXPENSES));
		}
		else return 0;
		
	}
	
	public void updateExpenses(int expenses){
		
		this.db = getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(COLUMN_CALC_EXPENSES, expenses);
		
		db.update(CALC_TABLE_NAME, values, null , null);
		
		db.close();
	}

}
